package com.android.gufengmall.my;

import android.os.Bundle;
import android.widget.EditText;

import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.App;
import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.adapter.AddressAdapter;
import com.android.gufengmall.bean.AddressBean;
import com.android.gufengmall.gen.AddressBeanDao;
import com.android.gufengmall.gen.DaoSession;

import java.util.List;

public class AddressActivity extends BaseActivity implements AddressAdapter.IDeleteLongClick{

    private RecyclerView mRvAddress;
    private AddressAdapter mAddressAdapter;
    private App mApp;
    private DaoSession mDaoSession;
    private AddressBeanDao mAddressBeanDao;
    private List<AddressBean> mAddressBeans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        findViewById(R.id.img_back).setOnClickListener(v -> {
            finish();
        });

        findViewById(R.id.img_add).setOnClickListener(v -> {
            final EditText edtAddress = new EditText(AddressActivity.this);
            AlertDialog.Builder inputDialog =
                    new AlertDialog.Builder(AddressActivity.this);
            inputDialog.setTitle("请输入您的地址").setView(edtAddress);
            inputDialog.setPositiveButton("确定", (dialog, which) -> {
                String getAddress = edtAddress.getText().toString().trim();
                AddressBean address = new AddressBean();
                address.setAddress(getAddress);
                mAddressBeans.add(address);
                mAddressBeanDao.insert(address);
                mAddressAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }).show();

        });
    }

    private void initData() {
        mApp = (App) getApplication();
        mDaoSession = mApp.getDaoSession();
        mAddressBeanDao = mDaoSession.getAddressBeanDao();
        mAddressBeans = mAddressBeanDao.loadAll();
        mRvAddress.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mAddressAdapter = new AddressAdapter(this, mAddressBeans);
        mAddressAdapter.setIDeleteLongClick(this);
        mRvAddress.setAdapter(mAddressAdapter);
    }

    private void initView() {
        mRvAddress = findViewById(R.id.rv_address);
    }

    @Override
    public void onLongClickDelete(int position) {
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(AddressActivity.this);
        inputDialog.setTitle("提示");
        inputDialog.setMessage("确认删除该地址吗？");
        inputDialog.setPositiveButton("确认", (dialog, which) -> {
            AddressBean addressBean = mAddressBeans.get(position);
            mAddressBeanDao.delete(addressBean);
            mAddressBeans.remove(position);
            mAddressAdapter.notifyDataSetChanged();
            showToast("删除成功");
            dialog.dismiss();
        });
        inputDialog.setNegativeButton("取消",null);
        inputDialog.show();
    }
}